import {
  getEmpNameList,
  recordAddCheck,
  recordAdd,
  getSearchRecordList,
  recordDic,
  deleteSearchRecordData,
  getDetail,
  recordUpdate,
  getHistorty,
} from '@/services/searchrecord';
import { message } from 'antd';

export default {
  state: {
    empNameList: [],
    formFieldsValue: {},
    expandForm: false,
    pageData: {
      // 列表数据
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    },
    status: [''],
    activeKey: '',
    state: '2',
    consultWay: undefined,
    historyArr: [],
  },

  effects: {
    // 查询人名
    *fetchEmpNameList({ payload }, { call, put }) {
      const response = yield call(getEmpNameList, payload);
      if (response.success) {
        if (response.data) {
          yield put({
            type: 'getEmpNameList',
            payload: response.data,
          });
        }
        if (!response.data.length) {
          message.warning('当前所输的不存在，请检查 ');
        }
      }
      return response;
    },
    // 新增校验
    *addCheckList({ payload, callback }, { call }) {
      const response = yield call(recordAddCheck, payload);
      if (response.success) {
        callback(response);
      } else {
        message.warning(response.message);
      }
    },

    // 添加数据
    addList:[
      function*({ payload, callback }, { call }) {
        const response = yield call(recordAdd, payload);
        if (response.success) {
          callback(response);
        } else {
          message.warning(response.message);
        }
      },
      { type:'throttle' , ms :10000 }
    ],
    // 修改
    updateDataList:[
      function*({ payload, callback }, { call, select }) {
        const { rcrdId, vno } = yield select(state => state.searchrecord);
        const response = yield call(recordUpdate, { ...payload, rcrdId, vno });
        if (response.success) {
          callback(response);
        } else {
          message.warning(response.message);
        }
      },
      { type:'throttle' , ms :10000 }
    ],
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(getSearchRecordList, payload);
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      yield put({
        type: 'updateList',
        payload: {
          data: [],
          currentPage,
          pageSize,
        },
      });
      if (response.success) {
        yield put({
          type: 'updateList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
        return response.data;
      }
      return message.warning(`${response.message || '服务端错误！'}`);
    },
    // 获取字典
    *getDic({ payload }, { call, put }) {
      const response = yield call(recordDic, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: response.data,
        });
        return response.data;
      }
      return message.warning(response.message);
    },
    // 删除
    *deleteData({ payload }, { call, put, select }) {
      const formFieldsValue = yield select(state => state.searchrecord.formFieldsValue);
      const activeKey = yield select(state => state.searchrecord.activeKey);
      const response = yield call(deleteSearchRecordData, payload);
      const values = {
        pageArg: { currentPage: 1, pageSize: 10 },
        consultRcrd: formFieldsValue,
        state: activeKey,
      };
      if (response.success) {
        yield put({
          type: 'fetchList',
          payload: values,
        });
      } else {
        message.warning(`${response.message || '服务端错误！'}`);
      }
    },
    // 获取详情
    *fetchDetail({ payload }, { call, put }) {
      const response = yield call(getDetail, payload);
      if (response.success) {
        yield put({
          type: 'getDetail',
          payload: response.data,
        });
        const res = yield call(getHistorty, payload);
        if (res.success) {
          yield put({
            type: 'getHistorty',
            payload: res.data,
          });
        } else {
          message.warning(res.message || '服务端错误！');
        }
      } else {
        message.warning(response.message || '服务端错误！');
      }
    },
  },

  reducers: {
    getEmpNameList(state, { payload }) {
      const empData = Array.isArray(payload) ? payload : [];
      return {
        ...state,
        empNameList: empData,
      };
    },
    // 获取列表
    updateList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];

      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 历史记录
    getHistorty(state, { payload }) {
      return {
        ...state,
        historyArr: Array.isArray(payload) ? payload : [],
      };
    },
    // 详情
    getDetail(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
